package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Professor;
import com.han.bom.Student;
import com.han.bom.utilities.Message;
import com.han.dao.concrete.product.UserDAO;

/**
 * Servlet implementation class ViewStudentServlet
 */
@WebServlet("/Professor/AddAndEditStudentServlet")
public class AddAndEditStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private Logger logger = Logger.getLogger(getClass().getName());

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddAndEditStudentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("Entered AddAndEditStudentServlet POST");

		Professor professor = null;
		UserDAO userdao = new UserDAO();
		Student student = null;
		String studentUserID = null,studentFirstName = null,studentLastName = null;

		HttpSession session = request.getSession();

		if(session.getAttribute("professor")!=null){
			professor = (Professor)session.getAttribute("professor");
		}
		else{

		}

		if(request.getParameter("studentFirstName")!=null){
			studentFirstName = request.getParameter("studentFirstName");
		}
		else{

		}


		if(request.getParameter("studentLastName")!=null){
			studentLastName = request.getParameter("studentLastName");
		}
		else{

		}

		int userID=-1;

		if(request.getParameter("studentUserID")!=null){
			studentUserID = request.getParameter("studentUserID");
		}
		else{

			try {
				userID = userdao.retrieveMaxUserID();
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(studentFirstName.length() == 0 || studentLastName.length()==0){
			response.sendRedirect("Student_View.jsp?msg=First+name+and+last+name+cannot+be+empty&msgtype=error");
			return;
		}
		

		if(userID==-1){

			student = professor.searchStudent(studentUserID);

			if(student!=null){

				student.setFirstName(studentFirstName);
				student.setLastName(studentLastName);

				userID = Integer.parseInt(student.getUserID().substring(1));
//				System.out.println("!!!!!"+studentFirstName.length()+" "+studentLastName.length());
				
				student.setUsername(studentFirstName.trim().toLowerCase().charAt(0)+studentLastName.toLowerCase().replace(" ","")+userID);

				try {
					userdao.updateUser(student);
				} catch (ClassNotFoundException e) {
					logger.error(e.getMessage(), e);
				} catch (SQLException e) {
					logger.error(e.getMessage(), e);
				} catch (NamingException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				}
				ArrayList<Message> messages = (ArrayList<Message>) session.getAttribute("messages");
				Message msg = new Message("Examinee "+studentFirstName+" "+studentLastName+ " Successfully Edited", "success");
				messages.add(0, msg);
				response.sendRedirect("Student_View.jsp?msg=Student+successfully+Edited&msgtype=success");
			}
			else{

			}
		}
		else{

			Student newStudent = new Student(userID);

			newStudent.setFirstName(studentFirstName);
			newStudent.setLastName(studentLastName);
			newStudent.setUsername(studentFirstName.trim().toLowerCase().charAt(0)+studentLastName.toLowerCase().replace(" ","")+userID);
			newStudent.setPassword("8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");

			professor.getStudents().add(newStudent);

			try {
				userdao.insertUser(newStudent);
				userdao.addStudentToProfessor(newStudent.getUserID(),professor.getUserID());
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (NamingException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			ArrayList<Message> messages = (ArrayList<Message>) session.getAttribute("messages");
			Message msg = new Message("Examinee "+studentFirstName+" "+studentLastName+ " Successfully Added", "success");
			messages.add(0, msg);
			response.sendRedirect("Student_New.jsp?msg=Student+Successfully+Added&msgtype=success");
		}
	}

}
